home *** CD-ROM | disk | FTP | other *** search
/ Aminet 44 / Aminet 44 (2001)(GTI - Schatztruhe)[!][Aug 2001].iso / Aminet / comm / mail / YAM23src.lha / Source / extrasrc / md5.c < prev    next >
C/C++ Source or Header  |  2000-12-15  |  9KB  |  276 lines

  1. #include <string.h>
  2. #include <md5.h>
  3. #ifdef _DCC
  4. #define __inline
  5. #endif
  6.  
  7. typedef unsigned char uint1;
  8. typedef UINT4 uint4;
  9.  
  10. void MD5Init(MD5_CTX *p)
  11. {
  12.    // Load magic initialization constants.
  13.    p->state[0] = 0x67452301;
  14.    p->state[1] = 0xefcdab89;
  15.    p->state[2] = 0x98badcfe;
  16.    p->state[3] = 0x10325476;
  17.  
  18.    // Nothing counted, so count=0
  19.    p->count[0] = 0;
  20.    p->count[1] = 0;
  21. }
  22.  
  23. #define S11 7
  24. #define S12 12
  25. #define S13 17
  26. #define S14 22
  27. #define S21 5
  28. #define S22 9
  29. #define S23 14
  30. #define S24 20
  31. #define S31 4
  32. #define S32 11
  33. #define S33 16
  34. #define S34 23
  35. #define S41 6
  36. #define S42 10
  37. #define S43 15
  38. #define S44 21
  39.  
  40. // Encodes input (UINT4) into output (unsigned char). Assumes len is
  41. // a multiple of 4.
  42. static __inline void encode(uint1 *output, uint4 *input, uint4 len)
  43. {
  44.    unsigned int i, j;
  45.  
  46.    for (i = 0, j = 0; j < len; i++, j += 4)
  47.    {
  48.       output[j]     = (uint1)  (input[i] & 0xff);
  49.       output[j + 1] = (uint1) ((input[i] >> 8) & 0xff);
  50.       output[j + 2] = (uint1) ((input[i] >> 16) & 0xff);
  51.       output[j + 3] = (uint1) ((input[i] >> 24) & 0xff);
  52.    }
  53. }
  54.  
  55. // Decodes input (unsigned char) into output (UINT4). Assumes len is
  56. // a multiple of 4.
  57. static __inline void decode(uint4 *output, uint1 *input, uint4 len)
  58. {
  59.    unsigned int i, j;
  60.  
  61.    for (i = 0, j = 0; j < len; i++, j += 4)
  62.       output[i] = ((uint4)input[j]) | (((uint4)input[j + 1]) << 8) |
  63.     (((uint4)input[j + 2]) << 16) | (((uint4)input[j + 3]) << 24);
  64. }
  65.  
  66. static __inline unsigned int rotate_left(uint4 x, uint4 n)
  67. {
  68.    return (x << n) | (x >> (32 - n));
  69. }
  70.  
  71. // F, G, H and I are basic MD5 functions.
  72.  
  73. static __inline unsigned int MD5_F(uint4 x, uint4 y, uint4 z)
  74. {
  75.    return (x & y) | (~x & z);
  76. }
  77.  
  78. static __inline unsigned int MD5_G(uint4 x, uint4 y, uint4 z)
  79. {
  80.    return (x & z) | (y & ~z);
  81. }
  82.  
  83. static __inline unsigned int MD5_H(uint4 x, uint4 y, uint4 z)
  84. {
  85.    return x ^ y ^ z;
  86. }
  87.  
  88. static __inline unsigned int MD5_I(uint4 x, uint4 y, uint4 z)
  89. {
  90.    return y ^ (x | ~z);
  91. }
  92.  
  93. // FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  94. // Rotation is separate from addition to prevent recomputation.
  95.  
  96. static __inline void MD5_FF(uint4* a, uint4 b, uint4 c, uint4 d, uint4 x,
  97.                 uint4 s, uint4 ac)
  98. {
  99.    *a += MD5_F(b, c, d) + x + ac;
  100.    *a = rotate_left(*a, s) + b;
  101. }
  102.  
  103. static __inline void MD5_GG(uint4* a, uint4 b, uint4 c, uint4 d, uint4 x,
  104.                 uint4 s, uint4 ac)
  105. {
  106.    *a += MD5_G(b, c, d) + x + ac;
  107.    *a = rotate_left(*a, s) + b;
  108. }
  109.  
  110. static __inline void MD5_HH(uint4* a, uint4 b, uint4 c, uint4 d, uint4 x,
  111.                 uint4 s, uint4 ac)
  112. {
  113.    *a += MD5_H(b, c, d) + x + ac;
  114.    *a = rotate_left(*a, s) + b;
  115. }
  116.  
  117. static __inline void MD5_II(uint4* a, uint4 b, uint4 c, uint4 d, uint4 x,
  118.                 uint4 s, uint4 ac)
  119. {
  120.    *a += MD5_I(b, c, d) + x + ac;
  121.    *a = rotate_left(*a, s) + b;
  122. }
  123.  
  124. // MD5 basic transformation. Transforms state based on block.
  125. static void transform(MD5_CTX *p, uint1 block[64])
  126. {
  127.    uint4 a = p->state[0], b = p->state[1], c = p->state[2], d = p->state[3], x[16];
  128.  
  129.    decode (x, block, 64);
  130.  
  131.    /* Round 1 */
  132.    MD5_FF(&a, b, c, d, x[ 0], S11, 0xd76aa478); /*  1 */
  133.    MD5_FF(&d, a, b, c, x[ 1], S12, 0xe8c7b756); /*  2 */
  134.    MD5_FF(&c, d, a, b, x[ 2], S13, 0x242070db); /*  3 */
  135.    MD5_FF(&b, c, d, a, x[ 3], S14, 0xc1bdceee); /*  4 */
  136.    MD5_FF(&a, b, c, d, x[ 4], S11, 0xf57c0faf); /*  5 */
  137.    MD5_FF(&d, a, b, c, x[ 5], S12, 0x4787c62a); /*  6 */
  138.    MD5_FF(&c, d, a, b, x[ 6], S13, 0xa8304613); /*  7 */
  139.    MD5_FF(&b, c, d, a, x[ 7], S14, 0xfd469501); /*  8 */
  140.    MD5_FF(&a, b, c, d, x[ 8], S11, 0x698098d8); /*  9 */
  141.    MD5_FF(&d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
  142.    MD5_FF(&c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  143.    MD5_FF(&b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  144.    MD5_FF(&a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  145.    MD5_FF(&d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  146.    MD5_FF(&c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  147.    MD5_FF(&b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  148.  
  149.    /* Round 2 */
  150.    MD5_GG(&a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
  151.    MD5_GG(&d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
  152.    MD5_GG(&c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  153.    MD5_GG(&b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
  154.    MD5_GG(&a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
  155.    MD5_GG(&d, a, b, c, x[10], S22,  0x2441453); /* 22 */
  156.    MD5_GG(&c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  157.    MD5_GG(&b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
  158.    MD5_GG(&a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
  159.    MD5_GG(&d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  160.    MD5_GG(&c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
  161.    MD5_GG(&b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
  162.    MD5_GG(&a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  163.    MD5_GG(&d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
  164.    MD5_GG(&c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
  165.    MD5_GG(&b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  166.  
  167.    /* Round 3 */
  168.    MD5_HH(&a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
  169.    MD5_HH(&d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
  170.    MD5_HH(&c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  171.    MD5_HH(&b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  172.    MD5_HH(&a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
  173.    MD5_HH(&d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
  174.    MD5_HH(&c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
  175.    MD5_HH(&b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  176.    MD5_HH(&a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  177.    MD5_HH(&d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
  178.    MD5_HH(&c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
  179.    MD5_HH(&b, c, d, a, x[ 6], S34,  0x4881d05); /* 44 */
  180.    MD5_HH(&a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
  181.    MD5_HH(&d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  182.    MD5_HH(&c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  183.    MD5_HH(&b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
  184.  
  185.    /* Round 4 */
  186.    MD5_II(&a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
  187.    MD5_II(&d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
  188.    MD5_II(&c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  189.    MD5_II(&b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
  190.    MD5_II(&a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  191.    MD5_II(&d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
  192.    MD5_II(&c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  193.    MD5_II(&b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
  194.    MD5_II(&a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
  195.    MD5_II(&d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  196.    MD5_II(&c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
  197.    MD5_II(&b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  198.    MD5_II(&a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
  199.    MD5_II(&d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  200.    MD5_II(&c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
  201.    MD5_II(&b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
  202.  
  203.    p->state[0] += a;
  204.    p->state[1] += b;
  205.    p->state[2] += c;
  206.    p->state[3] += d;
  207.  
  208.    // Zeroize sensitive information.
  209.    memset ( (uint1 *) x, 0, sizeof(x));
  210. }
  211.  
  212. void MD5Update(MD5_CTX *p, unsigned char *input, unsigned int input_length)
  213. {
  214.    uint4 input_index, buffer_index;
  215.    uint4 buffer_space;                // how much space is left in buffer
  216.  
  217.    // Compute number of bytes mod 64
  218.    buffer_index = (unsigned int)((p->count[0] >> 3) & 0x3F);
  219.  
  220.    // Update number of bits
  221.    if ((p->count[0] += ((uint4)input_length << 3))<((uint4)input_length << 3))
  222.       p->count[1]++;
  223.  
  224.    p->count[1] += ((uint4)input_length >> 29);
  225.  
  226.    buffer_space = 64 - buffer_index;  // how much space is left in buffer
  227.  
  228.    // Transform as many times as possible.
  229.    if (input_length >= buffer_space)
  230.    { // ie. we have enough to fill the buffer
  231.       // fill the rest of the buffer and transform
  232.       memcpy(p->buffer + buffer_index, input, buffer_space);
  233.       transform(p, p->buffer);
  234.  
  235.       // now, transform each 64-byte piece of the input, bypassing the buffer
  236.       for (input_index = buffer_space; input_index + 63 < input_length;
  237.        input_index += 64)
  238.      transform(p, input + input_index);
  239.  
  240.       buffer_index = 0;  // so we can buffer remaining
  241.    }
  242.    else
  243.       input_index = 0;     // so we can buffer the whole input
  244.  
  245.    // and here we do the buffering:
  246.    memcpy(p->buffer + buffer_index, input + input_index, input_length - input_index);
  247. }
  248.  
  249. void MD5Final(unsigned char digest[16], MD5_CTX *p)
  250. {
  251.    static uint1 PADDING[64]={
  252.       0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  253.       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  254.       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  255.    };
  256.    unsigned int index, padLen;
  257.    unsigned char bits[8];
  258.  
  259.    // Save number of bits
  260.    encode(bits, p->count, 8);
  261.  
  262.    // Pad out to 56 mod 64.
  263.    index = (uint4) ((p->count[0] >> 3) & 0x3f);
  264.    padLen = (index < 56) ? (56 - index) : (120 - index);
  265.    MD5Update(p, PADDING, padLen);
  266.  
  267.    // Append length (before padding)
  268.    MD5Update(p, bits, 8);
  269.  
  270.    // Store state in digest
  271.    encode(digest, p->state, 16);
  272.  
  273.    // Zeroize sensitive information
  274.    memset(p->buffer, 0, sizeof(p->buffer));
  275. }
  276.